# coding=utf-8
from MimicryHoneypot import *
import tensorflow as tf
import numpy as np
from DoubleDQNTrainer import *
from random import randint
import joblib
import random
from random import randint
class DoubleDQNEvolver:
    __honeypot  = None
    __dqn_model = None
    config_file_storage_path = '/etc/vsftpd/vsftpd_configs.pkl'
    def __init__(self,honeypot):
        self.__honeypot = honeypot
        self.__dqn_model = DoubleDQNTrainer.createDQN()
        self.__dqn_model.load_weights(DoubleDQNTrainer.model_path)
    def step(self):
        ip,configs = self.__honeypot.getCurrentConfig()
        last_state = {
            "ip":ip,
            "configs":configs
        }
        joblib.dump(last_state,self.config_file_storage_path)
        
        action_index = 0
        if random.random() < 0.7:
            action_space_len = self.__honeypot.action_space_len
            action_index = randint(0,action_space_len - 1)
        else:
            current_state = self.__honeypot.currentState()
            Q_values = self.__dqn_model.predict(current_state[np.newaxis])
            action_index = np.argmax(Q_values[0])
            
    
        self.__honeypot.step(action_index)
        


